﻿Imports System.Data.SqlClient.SqlParameter
Imports System.Data.SqlClient
Imports DTO

Public Class MatHangDAO
    Dim dp As New DataProvider()
    Function LoadMH() As DataTable
        Dim dtmathang As New DataTable
        dtmathang = dp.ExecuteQuery("LoadMatHang", Nothing)
        Return dtmathang
    End Function


    Function LoadDsSPTheoMA(ByVal masp As Integer) As DataTable
        Dim dsNhan As New DataTable
        Dim para As SqlParameter() = New SqlParameter(0) {}
        para(0) = New SqlParameter("MaSP", masp)
        dsNhan = dp.ExecuteQuery("LayMHTheoID", para)
        Return dsNhan
    End Function

    

    Sub Themsp(ByVal spdto As MatHangDTO)
        Dim dp As New DataProvider()
        Dim para As SqlParameter() = New SqlParameter(3) {}
        para(0) = New SqlParameter("@TenSP", spdto.TenSP)
        para(1) = New SqlParameter("@Ngaysx", spdto.NgaySanXuat)
        para(2) = New SqlParameter("@dongia", spdto.GiaBan)
        para(3) = New SqlParameter("@Maloai", spdto.MaLoai)
        dp.ExecuteNonQuery("InsertSP", para)
    End Sub

    Function LayTatCa() As Collections.IList
        Dim cn As New DataProvider()
        Dim array As New ArrayList()
        Dim dt = cn.ExecuteQuery("LayAllMH")
        Dim mh As New MatHangDTO()
        If dt.Rows.Count <> 0 Then
            For Each dr As DataRow In dt.Rows
                mh.MaSP = Integer.Parse(dr.Item("MaSP").ToString())
                mh.TenSP = dr.Item("TenSP").ToString()
                mh.NgaySanXuat = DateTime.Parse(dr.Item("NgaySanXuat").ToString())
                mh.MaLoai = Integer.Parse(dr.Item("MaLoai").ToString())
                mh.GiaBan = Integer.Parse(dr.Item("GiaBan").ToString())
                array.Add(mh)
            Next
        End If
        Return array
    End Function

    Function LayTatCaTT() As DataTable
        Dim cn As New DataProvider()
        Return cn.ExecuteQuery("LayAllMH")
    End Function


    Function laytheoID(ByVal id As Integer) As MatHangDTO
        Dim cn As New DataProvider()
        Dim para = New SqlParameter("@MaSP", id)
        Dim dt = cn.ExecuteQuery("LayMHTheoID", para)
        Dim mathang As New MatHangDTO()
        If dt.Rows.Count <> 0 Then
            mathang.MaSP = Integer.Parse(dt.Rows(0).Item("MaSP").ToString())
            mathang.TenSP = dt.Rows(0).Item("TenSP").ToString()
            mathang.NgaySanXuat = DateTime.Parse(dt.Rows(0).Item("NgaySanXuat").ToString())
            mathang.GiaBan = Integer.Parse(dt.Rows(0).Item("GiaBan").ToString())
            mathang.MaLoai = Integer.Parse(dt.Rows(0).Item("MaLoai"))
        End If
        Return mathang
    End Function

End Class
